$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/system/dlCompany/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("操作失败");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function status(status) {
	var companyId = $("#companyId").val();
	if(status == '2'){
		layer.open({
			id:1,
			type: 1,
			title:'请输入失败原因',
			area : [ '250px', '220px' ],
			style: 'width:80%;height:auto;',
			content: "<div required lay-verify=\"required\" style='display:flex;justify-content:center;'><textarea id='area' style='width:100%;height:135px;'></textarea></div>",
			btn:['确定','取消'],
			yes:function (index,layero) {
				//获取输入框里面的值
				var closeContent = top.$("#area").val() || $("#area").val();
				var ids = [];
				var els =document.getElementsByName("ids");
				for (var i = 0, j = els.length; i < j; i++){
					ids[i] = els[i].value; //获得值<br>alert(els[i].id);　　//获得ID
				}
				$.ajax({
					url: "/system/dlCompany/auditFailureText",
					type:"post",
					data: {
						id: companyId,
						auditFailureText: closeContent
					},
					success: function (res){
						$.ajax({
							cache : true,
							type : "POST",
							url : "/system/dlCompany/update",
							data : {
								companyId:companyId,
								ids:ids,
								status: status,
								auditStatus: status
							},
							async : false,
							error : function() {
								parent.layer.alert("操作失败");
							},
							success : function(res) {
								parent.layer.msg(res.message);
								parent.reLoad();
								var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
								parent.layer.close(index);
							}
						});
					}
				})
				layer.close(index);
				// 在这里提交数据
			},
			no:function (index,layero) {
				layer.close(index);
			}
		});
	}else{
		layer.open({
			id:1,
			type: 1,
			title:'确定审核通过吗？',
			area : [ '200px', '150px' ],
			style: 'width:80%;height:25px;',
			content: "<div style='display:flex;justify-content:center; align-items: center; width:100%;height:65px;'>确定审核成功吗？</div>",
			btn:['确定','取消'],
			yes:function (index,layero) {
				var ids = [];
				var els =document.getElementsByName("ids");
				for (var i = 0, j = els.length; i < j; i++){
					ids[i] = els[i].value; //获得值<br>alert(els[i].id);　　//获得ID
				}
				//获取输入框里面的值
				$.ajax({
					cache : true,
					type : "POST",
					url : "/system/dlCompany/update",
					data : {
						companyId:companyId,
						status: status,
						ids:ids,
						auditStatus: status
					},
					async : false,
					error : function() {
						parent.layer.alert("操作失败");
					},
					success : function(res) {
						parent.layer.alert(res.msg);
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
						parent.layer.close(index);
					}
				});

				layer.close(index);
				// 在这里提交数据
			},
			no:function (index,layero) {
				layer.close(index);
			}
		});

	}

}

function fileUpload(id, type){
	var photo=$("#"+id).val();
	if(photo!=null && photo!=""){
		// 构建数据
		var data = new FormData()
		data.append('type', type)
		data.append('name', $('#'+id).val())
		data.append('file', $('#'+id)[0].files[0]) // file 对象
		$.ajax({
			cache : false,
			type : "POST",
			url : "/upload",
			data :data,
			processData: false,//数据不被转换为字符串
			contentType: false,//上传文件时使用，避免 JQuery 对其操作
			async : false,
			dataType:"json",
			error : function(request) {
			},
			success : function(data) {
				if (data.code == 0) {
					photo = data.path;
					$("input[name='"+id+"']").val(data.newFileName);
					console.log(photo);
					$("#show"+id).attr("src",photo);
				}
			}
		});
	}

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}